package edu.pitt.myptm.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class DBUtils {
	public static int myhash(int id, int pageLimit) {
		return id % pageLimit;
	}

	public static boolean copyfile(String srFile, String dtFile) {
		try {
			File f1 = new File(srFile);
			File f2 = new File(dtFile);
			if(!f2.exists()) f2.createNewFile();
			InputStream in = new FileInputStream(f1);

			// For Append the file.
			// OutputStream out = new FileOutputStream(f2,true);

			// For Overwrite the file.
			OutputStream out = new FileOutputStream(f2);

			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
			return true;
		} catch (FileNotFoundException ex) {
			System.out
					.println(ex.getMessage() + " in the specified directory.");
			return false;
		} catch (IOException e) {
			System.out.println(e.getMessage());
			return false;
		}
	}
	
	public static boolean deleteFile(String filename) {
		File f = new File(filename);
		return f.delete();
	}
	
	public static String genFilename(String filename) {
		String newfilename = "";
		if(filename.contains(".txt")) {
			newfilename = filename.replace(".txt", ".mpm");
		} else {
			newfilename = filename+".mpm";
		}
		return newfilename;
	}
}
